﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.Configuration;

namespace LevenBlog.Core.Install.Operations {
    public class WebConfigItem {
        internal static string ConnectionName {
            get { return "BlogDbSetting"; }
        }

        internal static string PasswordKeyName {
            get { return "PasswordKey"; }
        }

        internal static string AdminPasswordKeyName {
            get { return "AdminPasswordKey"; }
        }

        internal static string PrefixKeyName {
            get { return "PrefixKey"; }
        }

        public string ConnectionString { get; set; }
        public string ProviderName { get; set; }
        public string PasswordKey { get; set; }
        public string AdminPasswordKey { get; set; }
        public string PrefixKey { get; set; }
    }

    public class WebConfigInstall :IInstallOperation {
        #region IInstallOperation Members

        public string Name {
            get { return CoreResource.install_webconfig_name; }
        }

        public string Intro {
            get { return CoreResource.install_webconfig_intro; }
        }

        public object Data {
            get;
            set;
        }

        public OperationResult Execute(OperationContext context) {
            List<string> errors = new List<string>();
            try {
                if (!(Data is WebConfigItem)) {
                    errors.Add(CoreResource.install_sqliteconfigupdate_dataerror);
                }
                WebConfigItem config = Data as WebConfigItem;
                Configuration webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(
                    System.Web.HttpContext.Current.Request.ApplicationPath);
                ConnectionStringsSection connSection = (ConnectionStringsSection)webConfig.GetSection("connectionStrings");
                var find = connSection.ConnectionStrings[WebConfigItem.ConnectionName];
                if (find == null) {
                    connSection.ConnectionStrings.Add(new ConnectionStringSettings() {
                        ConnectionString = config.ConnectionString,
                        Name = WebConfigItem.ConnectionName,
                        ProviderName = config.ProviderName
                    });
                } else {
                    find.ConnectionString = config.ConnectionString;
                    find.ProviderName = config.ProviderName;
                }
                AppSettingsSection appSection = (AppSettingsSection)webConfig.GetSection("appSettings");
                var passwordKeyFind = appSection.Settings[WebConfigItem.PasswordKeyName];
                if (passwordKeyFind == null) {
                    appSection.Settings.Add(WebConfigItem.PasswordKeyName, config.PasswordKey);
                } else {
                    passwordKeyFind.Value = config.PasswordKey;
                }
                var adminPasswordKeyFind = appSection.Settings[WebConfigItem.AdminPasswordKeyName];
                if (adminPasswordKeyFind == null) {
                    appSection.Settings.Add(WebConfigItem.AdminPasswordKeyName, config.AdminPasswordKey);
                } else {
                    adminPasswordKeyFind.Value = config.AdminPasswordKey;
                }
                var prefixKeyFind = appSection.Settings[WebConfigItem.PrefixKeyName];
                if (prefixKeyFind == null) {
                    appSection.Settings.Add(WebConfigItem.PrefixKeyName, config.PrefixKey);
                } else {
                    prefixKeyFind.Value = config.PrefixKey;
                }
                webConfig.Save();
            } catch (Exception e) {
                errors.Add(e.Message);
            }
            return new OperationResult(errors.Count > 0 ? false : true, errors);
        }

        #endregion
    }
}
